
$(function() {
	Tip.supplier();
	
	Tip.emp('负责人');
	
	$("input[id^='et']:visible").datepicker(_date1_option);
	
	$("select[id^='way_s']:visible").multiselect({
		minWidth: 140,
		selectedList: 4
	}).multiselect("uncheckAll");
	
	$("select[id^='payment_s']:visible").multiselect({
		minWidth: 140,
		selectedList: 4
	}).multiselect("uncheckAll");
});

var Plan = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}
				
				var form = $("#form_add")[0];

				var body = { };
				body.supplier = form.supplier.value;
				body.supplier_x = Supplier.supplier_x(form);
				
				body.way = form.way.value;
				body.payment = form.payment.value;
				
				body.cost = form.cost.value;
				body.currency = form.currency.value;
				
				body.dst = form.dst.value;
				body.voyage = form.voyage.value;
				body.number = form.number.value;
				
				body.ets = form.ets.value;
				body.etc = form.etc.value;
				body.etd = form.etd.value;
				body.eta = form.eta.value;
				body.date = form.date.value;
				
				body.content = form.content.value;
				body.emp = form.emp.value;
				
				$(this).dialog("close");
				
				$.post('express.php?do=Plan.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	express: function (id) {
		var url = "contact.php?do=Express.view&small=on&id=" + id;
		$('#dialog_view').dialog('option', 'buttons', { 		
			"关闭": function() { 
				$(this).dialog("close");
			}
		});

		dialog_view(url, '货运公司', '600x400');
	},
	
	popup: function (e, p2, id, out, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'express.php?do=Plan.edit&id=' + id + p2;
		var c2 = 'express.php?do=Plan.view&id=' + id;

		var cs = 'express.php?do=Plan.status&id=' + id + p2;
			
		var c5 = 'express.php?do=Plan.reason&id=' + id;
				
		var a1 = ['编辑信息', 'url', 'Plan.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Plan.dialog_view_', '650x400'];
			
		var a5 = ['暂停货运', 'url', 'Plan.dialog_stop', '650x400'];
		var a6 = ['取消货运', 'url', 'Plan.dialog_cancel', '650x400'];
		
		// 等待走货
		if (status == 1) {
			var b1 = ['开始走货', 'url', 'Plan.list_start'];
			
			var s = cs + "&status=2, " + c1 + ", " + c2 + ", " + c5 + ", " + c5;

			return this.popup_(e, [b1, a1, a2, a5, a6], s);
		}

		// 正在走货
		if (status == 2) {
			var d1 = 'express.php?do=Plan.finish&id=' + id;		
			var b1 = ['完成货运', 'url', 'Plan.dialog_finish', '650x400'];
			
			var s = d1 + ", " + c1 + ", " + c2;

			return this.popup_(e, [b1, a1, a2], s);
		}
		
		// 完成走货
		if (status == 3 && (out == 0 || out == null || out == '')) {
			var d1 = 'express.php?do=Plan.out&id=' + id;		
			var b1 = ['添加支出', 'url', 'Plan.dialog_out', '640x400'];
						
			var s = d1 + ", " + c2;

			return this.popup_(e, [b1, a2], s);
		}
		
		// 暂停走货
		if (status == 5) {		
			var d1 = 'express.php?do=Plan.status_continue&id=' + id + p2;
			var b1 = ['继续货运', 'url', 'Plan.list_continue'];
			
			var s = d1 + ", " + c2;

			return this.popup_(e, [b1, a2], s);
		}
		
		// 取消走货
		if (status == 6) {
			var d1 = 'express.php?do=Plan.status_restart&id=' + id + p2;
			var b1 = ['重启货运', 'url', 'Plan.list_restart'];
			
			var s = d1 + ", " + c2;

			return this.popup_(e, [b1, a2], s);
		}

		var s = c2;

		return this.popup_(e, [a2], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var c2= 'express.php?do=Plan.upload&id=' + id;
		var a2 = ['上传附件', 'url', 'Common.dialog_upload', '600x420'];
		
		var c3 = 'express.php?do=Plan.copy&id=' + id;
		var a3 = ['复制信息', 'url', 'Plan.dialog_copy_', '650x400'];
		
		var ss = 'express.php?do=Plan.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Plan.dialog_ss', '400x200'];
		
		s += ", " + c2 + ", " + c3 + ", " + ss;
		
		a.push(a2);
		a.push(a3);
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Plan.audit&item=140', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'express.php?do=Plan.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	/**
	 * 编辑
	 */
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.supplier = form.supplier.value;
						body.supplier_x = Supplier.supplier_x(form);
						
						body.way = form.way.value;
						body.payment = form.payment.value;
						
						body.cost = form.cost.value;
						body.currency = form.currency.value;
						
						body.dst = form.dst.value;
						body.voyage = form.voyage.value;
						body.number = form.number.value;
						
						body.ets = form.ets.value;
						body.etc = form.etc.value;
						body.etd = form.etd.value;
						body.eta = form.eta.value;
						body.date = form.date.value;
						
						body.content = form.content.value;
						body.emp = form.emp.value;

						$(this).dialog("close");

						var url = 'express.php?do=Plan.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_view_: function (url, title, xy) {
		$('#dialog_view').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"打印": function() {
				var id = $.cookie('id_');

				var url = 'express.php?do=Plan.view&tpl=doc&id=' + id;

				jswin(url, 'car_plan', '800x600');
			}
		});

		dialog_view(url, title, xy);
	},
	
	/**
	 * 复制
	 */
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				body.supplier = form.supplier.value;
				body.supplier_x = Supplier.supplier_x(form);
				
				body.payment = form.payment.value;
				body.way = form.way.value;
				body.cost = form.cost.value;
				body.currency = form.currency.value;
				
				body.dst = form.dst.value;
				body.voyage = form.voyage.value;
				body.number = form.number.value;
				body.etd = form.etd.value;
				body.eta = form.eta.value;
				
				body.date = form.date.value;
				body.content = form.content.value;
				body.emp = form.emp.value;

				$(this).dialog("close");

				var url = 'express.php?do=Plan.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	download: function (key) {
		if (!confirm("确认是否下载？若下载，建议先另存，再打开。")) {
			return false;
		}
		
		var url = 'express.php?do=Plan.download&key=' + key;
		jswin(url, 'save', '800x600');
		
		return true;
	},
	
	file: function (id, key) {
		if (!confirm("确认是否删除？")) {
			return false;
		}
		
		var c6 = 'express.php?do=Plan.upload&id=' + id;
		
		var url = 'express.php?do=Plan.file';
		var body = { };
		body.key = key;
		
		$.post(url, body, function(data) {
			// alert(data);
			$.get(c6, function(data) {
				$('#dialog_edit').html(data);
			});
		});
		
		return true;
	},

	/**
	 * 暂停
	 */
	dialog_stop: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否暂停？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						body.reason = form.reason.value;
						body.status = 5;

						$(this).dialog("close");

						var url = 'express.php?do=Plan.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 取消
	 */
	dialog_cancel: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否取消？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						body.reason = form.reason.value;
						body.status = 6;

						$(this).dialog("close");

						var url = 'express.php?do=Plan.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	list_start: function (url, view) {
		$.getJSON('express.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				if (confirm("是否开始走货？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	dialog_finish: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						
						body.eta = form.eta.value;
						body.date = form.date.value;
						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'express.php?do=Plan.finishPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_out: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"提交": function() {
				if (!confirm("确认是否添加支出信息？")) {
					return false;
				}

				var form = $("#form_add")[0];
						
				var body = { };
				body.id = form.id.value;
				body.supplier = form.supplier.value;				
				body.supplier_x = Supplier.supplier_x(form);
				
				body.name = form.name.value;
				body.type = form.type.value;
				
				body.currency = form.currency.value;
				body.amount = form.amount.value;
				body.rate = form.rate.value;
				body.rmb = form.rmb.value;
				
				body.date = form.date.value;
				body.emp = form.emp.value;
				body.content = form.content.value;

				$(this).dialog("close");
				
				var url = 'express.php?do=Plan.outPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	list_restart: function (url, view) {
		$.getJSON('express.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				if (confirm("是否要重启货运？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	list_continue: function (url, view) {
		$.getJSON('express.php?do=Plan.ajax_emp', function(json) {
			if (json.status == 1) {
				if (confirm("是否要继续货运？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'express.php?do=Plan.delete' + p2;
		var c2 = 'express.php?do=Plan.xls' + p2;
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];	
		var a2 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c2;

		return popup(e, [a1, a2], s);
	}
};
